using System;
using System.Data;
using System.Collections;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Filter;
using DevExpress.XtraGrid.Filter.Parser;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;

namespace TaskVision
{
	/// <summary>
	/// Summary description for GridLayoutPreserver.
	/// </summary>
	public class GridLayoutPreserver
	{
		protected GridView  m_GridView			= null;
		protected Hashtable m_arrColumns		= null;
		protected int[]		m_arrnSelectedRow;
		protected int		m_nVisibleRow;
		protected int		m_nEndVisibleRow;
	
		public GridLayoutPreserver(GridView PreservedGrid)
		{
			m_GridView = PreservedGrid;
		}

		public bool HasLayout
		{
			get
			{
				return (m_arrColumns != null);
			}
		}

		public void SaveLayout()
		{
			m_arrColumns = new Hashtable();

			foreach (GridColumn currCol in m_GridView.Columns)
			{
				m_arrColumns.Add(currCol.Name, GetExtendedProperties(currCol));
			}		
		}

		public void LoadLayout()
		{
			m_GridView.BeginUpdate();
			foreach (string colName in m_arrColumns.Keys)
			{
				SetExtendedProperties(m_GridView.Columns[colName], (Hashtable)m_arrColumns[colName]);
			}		
			m_GridView.EndUpdate();
			
			m_GridView.BeginUpdate();
			int nIdx;			
			int nEnd = m_arrColumns.Count - 1;
			string[] arrsVisible = new string[m_arrColumns.Count];
			foreach (string colName in m_arrColumns.Keys)
			{
				nIdx = Convert.ToInt32(((Hashtable)m_arrColumns[colName])["VIndex"]);
				if (nIdx != -1) 
				{
					arrsVisible[nIdx] = colName;
				}
				else
				{
					arrsVisible[nEnd--] = "";
				}
			}
			
			for (int nCurrIdx = 0; nCurrIdx < arrsVisible.Length; ++nCurrIdx)
			{
				try
				{
					if (arrsVisible[nCurrIdx] == "") break;
					else m_GridView.Columns[arrsVisible[nCurrIdx]].VisibleIndex = nCurrIdx;
				}
				catch{}
			}

			m_GridView.EndUpdate();
			m_arrColumns = null;
		}

		public void SaveSelectedRows()
		{
			m_arrnSelectedRow = m_GridView.GetSelectedRows();
			m_nVisibleRow = -1;

			int nIdx = m_GridView.TopRowIndex;
			while (nIdx != GridControl.InvalidRowHandle)
			{
				if ((m_GridView.IsRowVisible(nIdx) == RowVisibleState.Visible) && (m_nVisibleRow == -1))
				{
					m_nVisibleRow = nIdx;					
				}
				else if (((m_GridView.IsRowVisible(nIdx) == RowVisibleState.Hidden) || (m_GridView.IsRowVisible(nIdx) == RowVisibleState.Partially)) && (m_nVisibleRow != -1))
				{
					m_nEndVisibleRow = nIdx - 1;
					break;
				}
				nIdx = m_GridView.GetNextVisibleRow(nIdx);
			}			
		}

		public void LoadSelectedRows()
		{			
			try
			{
				m_GridView.ClearSelection();
				if ((m_arrnSelectedRow != null) && (m_arrnSelectedRow.Length != 0))
				{
					foreach (int currId in m_arrnSelectedRow)
					{
						m_GridView.FocusedRowHandle = currId;
						m_GridView.SelectRow(currId);
					}
				}
				m_GridView.MakeRowVisible(m_nEndVisibleRow, true);
				m_GridView.MakeRowVisible(m_nVisibleRow, true);
			}
			catch 
			{}
		}

		public void OrderColumns()
		{
			ArrayList arrColumns = new ArrayList(m_GridView.Columns.Count);
			Hashtable arrColsDef = new Hashtable();

			foreach (GridColumn currCol in m_GridView.Columns )
			{
				arrColumns.Add(currCol.Caption);
				arrColsDef.Add(currCol.Caption, currCol.FieldName);
			}
			arrColumns.Sort();
			m_GridView.BeginUpdate();

			for (int nCurrIdx = 0; nCurrIdx < arrColumns.Count; nCurrIdx++)
			{
				m_GridView.Columns[arrColsDef[arrColumns[nCurrIdx]].ToString()].AbsoluteIndex = nCurrIdx;
			}

			m_GridView.EndUpdate();
		}
		protected Hashtable GetExtendedProperties(GridColumn Column)
		{
			Hashtable arrExtndProp = new Hashtable();
						
			arrExtndProp.Add("AIndex", Column.AbsoluteIndex);
			arrExtndProp.Add("VIndex", Column.VisibleIndex);
			arrExtndProp.Add("Width", Column.Width);
			arrExtndProp.Add("GroupIndex", Column.GroupIndex);
			arrExtndProp.Add("SortIndex", Column.SortIndex);
			arrExtndProp.Add("SortOrder", Column.SortOrder);
			arrExtndProp.Add("ColumnEdit", Column.ColumnEdit);
			arrExtndProp.Add("ColumnOptions", Column.Options);
			arrExtndProp.Add("FormatType", Column.FormatType);
			arrExtndProp.Add("FormatString", Column.FormatString);
			arrExtndProp.Add("FilterString", Column.FilterInfo.FilterString);
			arrExtndProp.Add("FilterType", Column.FilterInfo.Type);
			arrExtndProp.Add("FilterValue", Column.FilterInfo.Value);
			arrExtndProp.Add("SummaryDisplayFormat", Column.SummaryItem.DisplayFormat);
			arrExtndProp.Add("SummaryFieldName", Column.SummaryItem.FieldName);
			arrExtndProp.Add("SummarySummaryType", Column.SummaryItem.SummaryType);

			return arrExtndProp;
		}

		protected void SetExtendedProperties(GridColumn Column, Hashtable ExtendedProperties)
		{
			try
			{
				Column.AbsoluteIndex = (int)ExtendedProperties["AIndex"];
				//			Column.VisibleIndex = (int)ExtendedProperties["VIndex"];
				Column.VisibleIndex = -1;
				Column.Width		= (int)ExtendedProperties["Width"];
				Column.GroupIndex   = (int)ExtendedProperties["GroupIndex"];
				Column.SortIndex	= (int)ExtendedProperties["SortIndex"];
				Column.SortOrder	= (ColumnSortOrder)ExtendedProperties["SortOrder"];

				Column.ColumnEdit   = (RepositoryItem)ExtendedProperties["ColumnEdit"];
				Column.Options		= (ColumnOptions)ExtendedProperties["ColumnOptions"];

				Column.FormatType   = (FormatTypeEnum)ExtendedProperties["FormatType"];
				Column.FormatString = (string)ExtendedProperties["FormatString"];
			
				Column.FilterInfo	= new ColumnFilterInfo((ColumnFilterType)ExtendedProperties["FilterType"], 
					ExtendedProperties["FilterValue"], 
					(string)ExtendedProperties["FilterString"]);
			
				Column.SummaryItem.ShowInColumn		= Column;
				Column.SummaryItem.DisplayFormat	= (string)ExtendedProperties["SummaryDisplayFormat"];
				Column.SummaryItem.FieldName		= (string)ExtendedProperties["SummaryFieldName"];
				Column.SummaryItem.SummaryType		= (SummaryItemType)ExtendedProperties["SummarySummaryType"];
			}
			catch{}
		}
	}
}
